package handler

import (
	"gopkg.in/mgo.v2/bson"
	"svvenus/conf"
	"svvenus/vgame/model"
	"svvenus/vpub/constants"
	"svvenus/znet"
	"svvenus/znet/zpb"
)

// CLUserLogin
// @Description: 用户登录
// @param request
func CLUserLogin(request *znet.Request) {
	rsp := &zpb.L2CUserLogin{}
	rid := zpb.EPid_LCUserLogin
	var req zpb.C2LUserLogin
	if !request.GetPb(&req) {
		return
	}

	db := constants.MgoMain.New()
	defer db.Close()

	user := &model.User{}
	err := db.Get(bson.M{"Account": req.Account}, user)
	if err != nil {
		user.Id = bson.NewObjectId()
		user.Account = req.Account
		user.Password = req.Password
		err = db.Create(user)
		if err != nil {
			request.Send(rid, conf.EProtocolResult服务器数据库异常, rsp)
			return
		}
	}
	//todo 生成token

	rsp.UserId = user.Id.Hex()

	request.Send(rid, 0, rsp)
}
